
#include <string>
#include <algorithm> // std::replace

namespace LIB
{
    char hexToChar(unsigned char input)
    {
        if (input < 10)
        {
            return '0' + input;
        }
        else
        {
            return 'A' + input - 10;
        }
    }
    unsigned char charToHex(char c)
    {
        if (c >= '0' && c <= '9')
            return c - '0';
        if (c >= 'a' && c <= 'f')
            return c - 'a' + 10;
        if (c >= 'A' && c <= 'F')
            return c - 'A' + 10;
        return 0;
    }

    std::string charArrayToHexString(const unsigned char *input, size_t inputLen)
    {
        std::string hexString;
        hexString.reserve(inputLen * 2); // 预分配足够的空间
        for (size_t i = 0; i < inputLen; ++i)
        {
            // hexString += "0x";
            hexString += hexToChar(input[i] / 16);
            hexString += hexToChar(input[i] % 16);
            // hexString += ", ";
        }
        return hexString;
    }

    std::string replaceAll(std::string &str, std::string oldStr, std::string newStr)
    {
        std::string::size_type pos = str.find(oldStr);
        while (pos != std::string::npos)
        {
            str.replace(pos, oldStr.size(), newStr);
            pos = str.find(oldStr);
        }
        return str;
    }

    int hexStringToCharArray(std::string hex, unsigned char *output, int outputsize)
    {
        uint8_t h, l;
        std::string a = replaceAll(hex, "0x", "");
        std::string b = replaceAll(a, " ", "");
        std::string c = replaceAll(b, "0X", "");
        const char *p = c.c_str();
        int s = c.size();
        s = s - (s % 2);
        if (outputsize < (s / 2))
            s = outputsize * 2;

        for (int i = 0; i < s / 2; i++)
        {
            h = charToHex(p[i * 2 + 0]);
            l = charToHex(p[i * 2 + 1]);
            output[i] = h << 4 | l;
        }
        return s;
    }

    // data[] 按 config[] 顺序 反转数据
    void reverse(uint8_t *data, int size)
    {
        uint8_t tmp = 0;
        for (int i = 0; i < size / 2; i++)
        {
            tmp = data[i];
            data[i] = data[size - i - 1];
            data[size - i - 1] = tmp;
        }
    }
    int big_little(uint8_t *data, uint8_t *config, int config_size)
    {
        int offset = 0;
        for (int j = 0; j < config_size; j++)
        {
            if (config[j] > 1)
                reverse(data + offset, config[j]);
            offset += config[j];
        }
        return offset;
    }

    //翻转 data
    void inverse_data(uint8_t *data, int len)
    {
        uint8_t tmp = 0;
        int i = 0;
        for (i = 0; i < len / 2; i++)
        {
            tmp = data[i];
            data[i] = data[len - 1 - i];
            data[len - 1 - i] = tmp;
        }
    }
    //将data翻转到  dataout
    void inverse_data_to(uint8_t *data, uint8_t *dataout, int len)
    {
        uint8_t tmp = 0;
        int i = 0;
        for (i = 0; i < len; i++)
        {
            dataout[i] = data[len - 1 - i];
        }
    }

};